package com.zixue.heima.helloNetty.EventLoopGroopDemo;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOutboundHandlerAdapter;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.ChannelPromise;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import lombok.extern.slf4j.Slf4j;

import java.nio.charset.StandardCharsets;

/**
 * @ClassName : NettyPileLineDemp
 * @Description : TODO
 * @Author : chunxiong
 * @Date : 2025/5/9 15:53
 * @Version 1.0
 */
@Slf4j
public class NettyPileLineDemp {
    public static void main(String[] args) {
        //pipeLine好比是流水线 handler 好比流水线上的工具
        //channelhanndle  入站 CHanelInboundHandler       出战CHanelOutboundHandler
        new ServerBootstrap()
                .group(new NioEventLoopGroup())
                .channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<NioSocketChannel>() {
                    @Override
                    protected void initChannel(NioSocketChannel ch) throws Exception {
                        //1.通过channel拿到pipeline
                        ChannelPipeline channelPipeline = ch.pipeline();
                        //2.添加处理器 netty辉帮我们多加两个个handler head ->   h1->->h2->h3 -》4 5 6 tail尾   底层是个双向链表
                           //   每次加 是在家tail 之前
                        //入站Handler 一般都关注读取
                        //这个这个Handler起个名字
                        channelPipeline.addLast("第1个参数给handler起个名字,handler1",new ChannelInboundHandlerAdapter(){                                                    
                            @Override
                            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                                log.debug("111111");
                                super.channelRead(ctx, msg);
                            }
                        });
                        channelPipeline.addLast("第1个参数给handler起个名字,handler2",new ChannelInboundHandlerAdapter(){
                            @Override
                            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                                log.debug("2222");
                                super.channelRead(ctx, msg);
                            }
                        });
                        channelPipeline.addLast("第1个参数给handler起个名字,handler3",new ChannelInboundHandlerAdapter(){
                            @Override
                            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                                log.debug("3333");
                                super.channelRead(ctx, msg);

                                //这些传站处理器 只有往channel里面写入数据才会出发 不写，OutboundHandler就形同虚设
                                //但是为什么在读里面写？
                                //ctx.alloc() 创建bytebuf.buffer
                                //这里是NioSocketChannel ch.writeAndFlush 不是ChannelHandlerContext ctx。writeAndFlush
                                ch.writeAndFlush(ctx.alloc().buffer().writeBytes("server..写入只为了触发出战处理器".getBytes()));
                            }
                        });
                        //出战处理器主要处理write方法
                        channelPipeline.addLast("第1个参数给handler起个名字，出战处理器，handler4", new ChannelOutboundHandlerAdapter(){

                            @Override
                            public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
                                log.debug("出站write 4444");
                                super.write(ctx, msg, promise);
                            }
                        });//出战处理器主要处理write方法
                        channelPipeline.addLast("第1个参数给handler起个名字，出战处理器，handler5", new ChannelOutboundHandlerAdapter(){

                            @Override
                            public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
                                log.debug("出站write 5555");
                                super.write(ctx, msg, promise);
                            }
                        });
                        //出战处理器主要处理write方法
                        channelPipeline.addLast("第1个参数给handler起个名字，出战处理器，handler6", new ChannelOutboundHandlerAdapter(){

                            @Override
                            public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
                                log.debug("出站write 6666");
                                super.write(ctx, msg, promise);
                            }
                        });
                    }
                })
                .bind(8080);
         //出战是尾巴往前走
        // 出站write 6666
        //出站write 5555
       //  出站write 4444


    }
}
